home *** CD-ROM | disk | FTP | other *** search
/ No Fragments Archive 12: Textmags & Docs / nf_archive_12.iso / MAGS / SOURCES / ATARI_SRC.ZIP / atari source / DSHJ2 / KEYCURS.C < prev    next >
Encoding:
C/C++ Source or Header  |  2001-02-10  |  12.7 KB  |  460 lines

  1. /*
  2.     This module implements the Text window
  3.     Scrolling, Cursor and Keyboard handling.
  4. */
  5. #include    <osbind.h>
  6. #include    <gemdefs.h>
  7. #include    <obdefs.h>
  8.  
  9. extern    int    txt_handle;        /* Text Window        handle    */
  10. extern    int    shandle;        /* virtual workstation    handle    */
  11. extern    GRECT    twork;            /* Text Window    area        */
  12. extern    int    sdevm;            /* Screen device mode        */
  13. extern    int    gl_wchar;        /* system text char width    */
  14. extern    int    gl_hchar;        /* system text char height    */
  15. extern    struct    {
  16.     char    mrow, mcol;            /* window Maximn RowCol    */
  17.     char    row, col;            /* window cursor RowCol    */
  18. } current;
  19. extern    int    TWhclip;            /* TWindow horiz. clip    */
  20.  
  21. #define        SLOR        0    /* Low    Res    320  x 200     */
  22. #define        SMDR        1    /* Medium Res    640  x 200     */
  23. #define        SHIR        2    /* High   Res    640  x 400    */
  24.  
  25. /*    ..Key translations..    */
  26. /*    All cursor keys        */
  27. #define        rtch        0x0c0    /* Cursor Right    */
  28. #define        lfch        0x0c1    /* Cursor Left    */
  29. #define        upch        0x0c2    /* Cursor Up    */
  30. #define        dnch        0x0c3    /* Cursor Down    */
  31. #define        bolc        0x0c4    /* S Curs Left    */
  32. #define        eolc        0x0c5    /* S Curs Right    */
  33. #define        pgup        0x0c6    /* S Curs Up    */
  34. #define        pgdn        0x0c7    /* S Curs Down    */
  35. #define        bofc        0x0c8    /* C Curs Up    */
  36. #define        eofc        0x0c9    /* C Curs Down    */
  37. #define        Insc        0x0ca    /* Insert Key    */
  38. #define        Delc        0x0cb    /* Delete Key    */
  39. #define        NxtW        0x0cc    /* C Curs Right    */
  40. #define        PrvW        0x0cd    /* C Curs Left    */
  41. /*    Ctrl Fctn keys        */
  42. #define        CF1        0x000    /* Cntrl F. 1    */
  43. #define        CF2        0x000    /* Cntrl F. 2    */
  44. #define        CF3        0x000    /* Cntrl F. 3    */
  45. #define        CF4        0x000    /* Cntrl F. 4    */
  46. #define        CF5        0x000    /* Cntrl F. 5    */
  47. #define        CF6        0x000    /* Cntrl F. 6    */
  48. #define        CF7        0x000    /* Cntrl F. 7    */
  49. #define        CF8        0x000    /* Cntrl F. 8    */
  50. #define        CF9        0x000    /* Cntrl F. 9    */
  51. #define        CF10        0x000    /* Cntrl F. 10    */
  52. /*    Shift Fctn keys        */
  53. #define        SF1        0x000    /* Shift F. 1    */
  54. #define        SF2        0x000    /* Shift F. 2    */
  55. #define        SF3        0x000    /* Shift F. 3    */
  56. #define        SF4        0x000    /* Shift F. 4    */
  57. #define        SF5        0x000    /* Shift F. 5    */
  58. #define        SF6        0x000    /* Shift F. 6    */
  59. #define        SF7        0x000    /* Shift F. 7    */
  60. #define        SF8        0x000    /* Shift F. 8    */
  61. #define        SF9        0x000    /* Shift F. 9    */
  62. #define        SF10        0x000    /* Shift F. 10    */
  63. /*    Alt Fctn keys        */
  64. #define        AF1        0x000    /* Alt   F. 1    */
  65. #define        AF2        0x000    /* Alt   F. 2    */
  66. #define        AF3        0x000    /* Alt   F. 3    */
  67. #define        AF4        0x000    /* Alt   F. 4    */
  68. #define        AF5        0x000    /* Alt   F. 5    */
  69. #define        AF6        0x000    /* Alt   F. 6    */
  70. #define        AF7        0x000    /* Alt   F. 7    */
  71. #define        AF8        0x000    /* Alt   F. 8    */
  72. #define        AF9        0x000    /* Alt   F. 9    */
  73. #define        AF10        0x000    /* Alt   F. 10    */
  74. /*    Function keys        */
  75. #define        F1        0x007    /* Disc Hyphen    */
  76. #define        F2        0x0f0    /* EM    dash    */
  77. #define        F3        0x0f7    /* EN    dash    */
  78. #define        F4        0x00c    /* Region Feed    */
  79. #define        F5        0x0fe    /* EM    space    */
  80. #define        F6        0x01c    /* Quad Right    */
  81. #define        F7        0x0fd    /* EN    space    */
  82. #define        F8        0x01d    /* Quad Center    */
  83. #define        F9        0x0fc    /* THIN    space    */
  84. #define        F10        0x012    /* Quad Left    */
  85. /*    DSII Special Keys    */
  86. #define        ctlC        0x003    /* Control C    */
  87. #define        sHY        0x00b    /* soft Hyphen    */
  88. #define        srt        0x00f    /* soft Return    */
  89. #define        sDH        0x014    /* soft DHyphen    */
  90. #define        esc        0x01b    /* Escape Key    */
  91. #define        hrt        0x01f    /* Hard Return    */
  92. #define        QR        0x01c    /* Quad Right    */
  93. #define        QC        0x01d    /* Quad Center    */
  94. #define        QL        0x012    /* Quad Left    */
  95. #define        DH        0x007    /* Disc Hyphen    */
  96. #define        TSP        0x0fc    /* THIN    space    */
  97. #define        NSP        0x0fd    /* EN    space    */
  98. #define        MSP        0x0fe    /* EM    space    */
  99. /*    Alernate Commands    */
  100. #define        ATA        0x0d0    /* Again    */
  101. #define        ATB        0x0d1    /* Buffer    */
  102. #define        ATC        0x0d2    /* Copy        */
  103. #define        ATD        0x0d3    /* Delete    */
  104. #define        ATF        0x0d4    /* Forward Find    */
  105. #define        ATG        0x0d5    /* Get Text    */
  106. #define        ATJ        0x0d6    /* Jump Marker    */
  107. #define        ATM        0x0d7    /* Set    Marker    */
  108. #define        ATR        0x0d8    /* Replace    */
  109. #define        ATS        0x0d9    /* Select Art.    */
  110. #define        ATT        0x0da    /* Insert G.Tag    */
  111. #define        ATV        0x0db    /* Reverse Find    */
  112. #define        ATX        0x0dc    /* Delete Tag    */
  113. #define        ATL        0x0de    /* Insert L.Tag    */
  114. #define        ATP        0x0df    /* Insert PiTag    */
  115.  
  116. /*    Keyboard Tables        */
  117. static    unsigned char    uns_cap[32] = {        /* Unshift/CapLock keys    */
  118.     0x00, ' ' , 0x00, F1  , F2  , F3  , F4  , F5  ,    /* 0x38.. 0x3f    */
  119.     F6  , F7  , F8  , F9  , F10 , 0x00, 0x00, 0x00,    /* 0x40.. 0x47    */
  120.     upch, 0x00, '-' , lfch, 0x00, rtch, '+' , 0x00,    /* 0x48.. 0x4f    */
  121.     dnch, 0x00, Insc, Delc, 0x00, 0x00, 0x00, 0x00    /* 0x50.. 0x57    */
  122. };
  123. static    unsigned char    shifted[24] = {        /* Shifted keys        */
  124.     pgup, 0x00, '-' , bolc, 0x00, eolc, '+' , 0x00,    /* 0x48.. 0x4f    */
  125.     pgdn, 0x00, Insc, Delc, SF1 , SF2 , SF3 , SF4 ,    /* 0x50.. 0x57    */
  126.     SF5 , SF6 , SF7 , SF8 , SF9 , SF10, 0x00, 0x00    /* 0x58.. 0x5f    */
  127. };
  128. static    unsigned char    contrl[80] = {        /* Control keys        */
  129.     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, ctlC, 0x00,    /* 0x28..0x2f    */
  130.     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,    /* 0x30..0x37    */
  131.     0x00, 0x00, 0x00, CF1 , CF2 , CF3 , CF4 , CF5 ,    /* 0x38..0x3f    */
  132.     CF6 , CF7 , CF8 , 0x00, CF10, 0x00, 0x00, 0x00,    /* 0x40..0x47    */
  133.     bofc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,    /* 0x48..0x4f    */
  134.     eofc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,    /* 0x50..0x57    */
  135.     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,    /* 0x58..0x5f    */
  136.     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,    /* 0x60..0x67    */
  137.     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,    /* 0x68..0x6f    */
  138.     0x00, 0x00, 0x00, PrvW, NxtW, 0x00, 0x00, 0x00    /* 0x70..0x77    */
  139. };
  140. static    unsigned char    altern[56] = {        /* Alternate keys    */
  141.     0x00, 0x00, 0x00, ATR , ATT , 0x00, 0x00, 0x00,    /* 0x10..0x17    */
  142.     0x00, ATP , 0x00, 0x00, 0x00, 0x00, ATA , ATS ,    /* 0x18..0x1f    */
  143.     ATD , ATF , ATG , 0x00, ATJ , 0x00, ATL , 0x00,    /* 0x20..0x27    */
  144.     0x00, 0x00, 0x00, 0x00, 0x00, ATX , ATC , ATV ,    /* 0x28..0x2f    */
  145.     ATB , 0x00, ATM , 0x00, 0x00, 0x00, 0x00, 0x00,    /* 0x30..0x37    */
  146.     0x00, 0x00, 0x00, AF1 , AF2 , AF3 , AF4 , AF5 ,    /* 0x38..0x3f    */
  147.     AF6 , AF7 , AF8 , AF9 , AF10, 0x00, 0x00, 0x00    /* 0x40..0x47    */
  148. };
  149.  
  150. /*
  151.     Routine to sound beeper.
  152. */
  153. #define        CON    2
  154. beep()
  155. {
  156.     Bconout(CON,7);
  157. }
  158.  
  159. /*
  160.     Routine to flush keyboard.
  161. */
  162. flush_kbd()
  163. {
  164. /*    while (Bconstat(CON)) Bconin(CON);*/
  165.      int tmp_buff[8];
  166.      int event;
  167.      int dummy;
  168.  
  169.            for(;;)    /* used to bleed off unwanted messages...*/
  170.            {
  171.         event = evnt_multi((MU_KEYBD|MU_TIMER),
  172.                     0,0,0,
  173.                     0,0,0,0,0,
  174.                     0,0,0,0,0,
  175.                     tmp_buff,
  176.                     0,0,        /* timer == 0 */
  177.                     &dummy,&dummy,
  178.                     &dummy,&dummy,
  179.                     &dummy,&dummy);
  180.  
  181.         if(event == MU_TIMER)
  182.                 break;
  183.            }
  184. }
  185.  
  186. /*
  187.     Function to map in our keyboard codes.
  188.     Used after an "event keyboard".
  189. */
  190. unsigned char    mapkbd(kstat,kbret)
  191. unsigned kstat, kbret;
  192. {
  193.     union {
  194.         struct {
  195.           unsigned char scan;    /* Kbd Scan code    */
  196.           unsigned char mchr;    /* mapped key code    */
  197.         }        kbyt;
  198.         unsigned    kret;
  199.     }    k;
  200.     k.kret = kbret;
  201.     kstat &= 15;
  202.     if (kstat == 0) {        /* Unshift/Capslock    */
  203.         if (k.kbyt.scan >= 0x38 && k.kbyt.scan <= 0x57)
  204.             k.kbyt.mchr    = uns_cap[k.kbyt.scan - 0x38];
  205.     }
  206.     else
  207.     if (kstat >= 1 && kstat <= 3) {    /* All Shifted...    */
  208.         if (k.kbyt.scan >= 0x48 && k.kbyt.scan <= 0x5f)
  209.             k.kbyt.mchr    = shifted[k.kbyt.scan - 0x48];
  210.     }
  211.     else
  212.     if (kstat == 4) {        /* Control only...    */
  213.         if (k.kbyt.scan >= 0x28 && k.kbyt.scan <= 0x77)
  214.             k.kbyt.mchr    = contrl[k.kbyt.scan - 0x28];
  215.         else    k.kbyt.mchr    = 0;
  216.     }
  217.     else
  218.     if (kstat == 8) {        /* Alternate only...    */
  219.         if (k.kbyt.scan >= 0x10 && k.kbyt.scan <= 0x47)
  220.             k.kbyt.mchr    = altern[k.kbyt.scan - 0x10];
  221.         else    k.kbyt.mchr    = 0;
  222.     }
  223.     else    k.kbyt.mchr    = 0;    /* other combo ignore    */
  224.     return(k.kbyt.mchr);
  225. }
  226.  
  227. /*    Screen Definitions    */
  228. #define        wdw_cols    75        /* # columns fixed...    */
  229. extern    char    wdw_rows;            /* # rows set by screen    */
  230.  
  231. /*    Cursor Tables        */
  232. static    unsigned tcurMR[10] = {            /* thin cursor MED RES    */
  233. 0x8000,0x8000,0x8000,0x8000,0x8000,0x8000,0x8000,0x8000,/* 0x00.. 0x07    */
  234. 0x8000,0x8000                        /* 0x08.. 0x09    */
  235. };
  236. static    unsigned tcurHR[18] = {            /* thin cursor HI RES    */
  237. 0x8000,0x8000,0x8000,0x8000,0x8000,0x8000,0x8000,0x8000,/* 0x00.. 0x07    */
  238. 0x8000,0x8000,0x8000,0x8000,0x8000,0x8000,0x8000,0x8000,/* 0x08.. 0x0f    */
  239. 0x8000,0x8000                        /* 0x10.. 0x11    */
  240. };
  241. static    unsigned bcurMR[10] = {            /* box cursor  MED RES    */
  242. 0xff00,0x8100,0x8100,0x8100,0x8100,0x8100,0x8100,0x8100,/* 0x00.. 0x07    */
  243. 0x8100,0xff00                        /* 0x08.. 0x09    */
  244. };
  245. static    unsigned bcurHR[18] = {            /* box cursor  HI RES    */
  246. 0xff00,0x8100,0x8100,0x8100,0x8100,0x8100,0x8100,0x8100,/* 0x00.. 0x07    */
  247. 0x8100,0x8100,0x8100,0x8100,0x8100,0x8100,0x8100,0x8100,/* 0x08.. 0x0f    */
  248. 0x8100,0xff00                        /* 0x08.. 0x09    */
  249. };
  250.  
  251. /*    Cursor variables    */
  252. static    unsigned *tcurs[3]= { tcurHR, tcurMR, tcurHR };
  253. static    unsigned *bcurs[3]= { bcurHR, bcurMR, bcurHR };
  254. static    unsigned crsxy[8] = { 0,0,7,0,0,0,0,0 };/* pxy table of cursor    */
  255. static    int    vrtcoi[2] = {BLACK,WHITE};    /* vrt_cpyfm colorIndex    */
  256. static    FDB    curs_MFDB;            /* cursor MFDB        */
  257. static    char    cursor_clr = 1;            /* cleared state    */
  258.  
  259. /*
  260.     Function to scroll up or down 1 line the window.
  261. */
  262. wdw_scroll(up)
  263. int    up;
  264. {
  265.     GRECT    s, d;
  266.     long    scrmfdb = 0L;            /* screen MFDB address    */
  267.  
  268.     s.g_y    = d.g_y    = twork.g_y;
  269.     if (up)
  270.         s.g_y    += gl_hchar;
  271.     else    d.g_y    += gl_hchar;
  272.     s.g_x    = d.g_x    = twork.g_x;
  273.     s.g_w    = d.g_w    = twork.g_w;
  274.     s.g_h    = d.g_h    = (current.mrow - 1) * gl_hchar;
  275.     rast_op(3,&s,&scrmfdb,&d,&scrmfdb);
  276.     if (up)                    /* adjust old cursor Y    */
  277.         crsxy[5] -= gl_hchar;
  278.     else    crsxy[5] += gl_hchar;
  279. }
  280.  
  281. /*
  282.     Function to clear Text Window area
  283. */
  284. clrTW(area)
  285. GRECT     *area;
  286. {
  287.     long    scrmfdb = 0L;            /* screen MFDB address    */
  288.  
  289.     rast_op(0,area,&scrmfdb,area,&scrmfdb);
  290. }
  291.  
  292. /*
  293.     Function to set maximum number of Row and Column
  294.     from current work area of Text Window.
  295. */
  296. setMrowcol()
  297. {
  298.     int    newmcol;
  299.  
  300.     current.mrow = twork.g_h / gl_hchar;
  301.     if (current.mrow > wdw_rows)
  302.         current.mrow = wdw_rows;
  303.     newmcol = (twork.g_w / gl_wchar) - 2;
  304.     if (newmcol == current.mcol)
  305.         return;
  306.     if (newmcol >= wdw_cols) {
  307.         newmcol = wdw_cols;
  308.         TWhclip = 0;
  309.     }
  310.     else
  311.     if (newmcol > current.mcol && TWhclip)
  312.         TWhclip -= (TWhclip <= (newmcol - current.mcol))
  313.                 ? TWhclip:newmcol - current.mcol;
  314.     current.mcol = newmcol;
  315. }
  316.  
  317. /*
  318.     Function to get R.C. X value of cursor from column.
  319. */
  320. xcrs(col)
  321. int    col;
  322. {
  323.     return(twork.g_x + ((col+1) * gl_wchar));
  324. }
  325.  
  326. /*
  327.     Function to get R.C. Y value of cursor from row.
  328. */
  329. ycrs(row)
  330. int    row;
  331. {
  332.     return(twork.g_y + (row * gl_hchar));
  333. }
  334.  
  335. /*
  336.     Function to check if point (x,y) are in Text Window area
  337. */
  338. xyinTW(x,y)
  339. int    x, y;
  340. {
  341.     return(    x >= twork.g_x && x < (twork.g_x + twork.g_w)
  342.         &&
  343.         y >= twork.g_y && y < (twork.g_y + twork.g_h) );
  344. }
  345.  
  346. /*
  347.     Function to set cursor on screen at R.C. x and y.
  348. */
  349. static    gcursor(y,x,mod)
  350. unsigned y, x, mod;
  351. {
  352.     GRECT    t1, s;
  353.     long    scrmfdb = 0L;        /* screen MFDB address    */
  354.  
  355.     if (!xyinTW(x,y))
  356.         return;
  357.     crsxy[4] = s.g_x = x;
  358.     crsxy[5] = s.g_y = y;
  359.     crsxy[6] = x + 7;
  360.     crsxy[7] = y + crsxy[3];
  361.     s.g_w     = 8;
  362.     s.g_h     = crsxy[3] + 1;
  363.     wind_get(txt_handle,WF_FIRSTXYWH,&t1.g_x,&t1.g_y,&t1.g_w,&t1.g_h);
  364.     while (t1.g_w && t1.g_h) {
  365.       if (rc_intersect(&s,&t1)) {
  366.         set_clip(1,&t1);
  367.         vrt_cpyfm(shandle,3,crsxy,&curs_MFDB,&scrmfdb,vrtcoi);
  368.         set_clip(0,&t1);
  369.         break;
  370.       }
  371.       wind_get(txt_handle,WF_NEXTXYWH,&t1.g_x,&t1.g_y,&t1.g_w,&t1.g_h);
  372.     }
  373.     cursor_clr = mod;        /* set cursor state    */
  374. }
  375.  
  376. /*
  377.     Function to setup the cursor variables.
  378. */
  379. setupcurs()
  380. {
  381.     curs_MFDB.fd_w        = 16;
  382.     curs_MFDB.fd_h        = sdevm == SMDR ? 10:18;
  383.     curs_MFDB.fd_wdwidth    = 1;
  384.     curs_MFDB.fd_stand    = 0;
  385.     curs_MFDB.fd_nplanes    = 1;
  386.     curs_MFDB.fd_addr    = (long)tcurs[sdevm];
  387.     crsxy[3]        = curs_MFDB.fd_h - 1;
  388. }
  389.  
  390. /*
  391.     Function to turn thin line cursor on. (System/Insert modes)
  392. */
  393. wdw_crsnm()
  394. {
  395.     setcursor(1);
  396. }
  397.  
  398. /*
  399.     Function to turn box type cursor on. (Overwrite mode)
  400. */
  401. wdw_crsbx()
  402. {
  403.     setcursor(2);
  404. }
  405.  
  406. /*
  407.     Function to set the cursor at current internal row and column.
  408.     "mode" if true change the cursor type :
  409.         1 --    thin line    cursor
  410.         2 --    box type    cursor
  411. */
  412. setcursor(mode)
  413. int    mode;
  414. {
  415.     clrcursor();                    /* delete old    */
  416.     if (mode == 1)
  417.         curs_MFDB.fd_addr = (long)tcurs[sdevm];
  418.     else
  419.     if (mode == 2)
  420.         curs_MFDB.fd_addr = (long)bcurs[sdevm];
  421.     gcursor(ycrs(current.row),xcrs(current.col),0);    /* display new    */
  422. }
  423.  
  424. /*
  425.     Function to clear old cursor if not cleared.
  426. */
  427. clrcursor()
  428. {
  429.     if (!cursor_clr) gcursor(crsxy[5],crsxy[4],1);
  430. }
  431.  
  432. /*
  433.     Function to convert if needed passed char to video one.
  434.     Returns new/old video character.
  435. */
  436. unsigned char    ctvdc(c)
  437. unsigned char    c;
  438. {
  439.     if (c == sHY ||
  440.         c == sDH)    c = '-';
  441.     else
  442.     if (c ==  DH)    c = 0x0fa;
  443.     else
  444.     if (c == srt)    c = 0x00d;
  445.     else
  446.     if (c == hrt)    c = 0x002;
  447.     else
  448.     if (c == QL)    c = 0x004;
  449.     else
  450.     if (c == QR)    c = 0x003;
  451.     else
  452.     if (c == QC)    c = 0x005;
  453.     else
  454.     if (c == ATP)    c = 0x0e3;
  455.     else
  456.     if (c == 0 ||
  457.         c == 255)    c = 0x07f;
  458.     return(c);
  459. }
  460.